<?

class Smutty_Controller {

	var $_viewData = array();

	/**
	 *  STATIC
	 *
	 *  returns the current controller
	 *
	 *  @return Smutty_Controller
	 *
	 */

	function &getCurrentController() {
		return Smutty_Controller::setCurrentController( $I_DONT_EXIST );
	}

	/**
	 *  STATIC
	 *
	 *  sets and/or returns a controller
	 *
	 *  @param [controller] optional controller
	 *  @return Smutty_Controller
	 *
	 */

	function &setCurrentController( &$ctlr ) {
		static $controller;
		if ( $controller == null )
			$controller = $ctlr;
		return $controller;
	}

	/**
	 *  shows the view file using the specified template class
	 *
	 *  @param [viewFile] path to view file
	 *  @param [template] instance of templace class to use
	 *
	 */

	function _view( $viewFile, $template ) {
		foreach ( $this->_viewData as $key => $value )
			$template->assign( $key, $value );
		$template->display( $viewFile );
	}

	/**
	 *  show the view file using the smutty template class
	 *
	 *  @param [viewFile] the file to view
	 *
	 */

	function _viewSmut( $viewFile ) {
		$this->_view( $viewFile, new Smutty_Template_Smutty() );
	}

	/**
	 *  show the specified file with the standard user
	 *  template class.  if the view file is not specifed
	 *  then it is assumed to be located as "views/controller/action.tpl"
	 *
	 *  @param [viewFile] the file to view
	 *
	 */

	function view( $viewFile = false ) {
		if ( !$viewFile ) {
			$router = Smutty_Router::getInstance();
			$viewFile = $router->getActionName() . '.tpl';
		}
		$this->_view( $viewFile, new Smutty_Template() );
	}

	/**
	 *  sets a variable for use by the template
	 *
	 *  @param [name] name of the variable to set
	 *  @param [value] the value to set
	 *
	 */

	function set( $name, $value ) {
		$this->_viewData[ $name ] = $value;
	}

	/**
	 *  returns the name of a set template variable
	 *
	 */

	function get( $name ) {
		return $this->_viewData[ $name ];
	}

	/**
	 *  adds an error message to the controller
	 *
	 *  @param [message] desc of error
	 *
	 */

	function addError( $message ) {
		if ( !is_array($this->_viewData['errors']) )
			$this->_viewData['errors'] = array();
		array_push( $this->_viewData['errors'], $message );
	}

	/**
	 *  redirects the user to the given url.  the parameter array
	 *  should be a hash and accepts the following values.
	 *
	 *    controller => name of controller
	 *    action => action on controller
	 *    id => id to pass to action
	 *
	 *  @param [params] array of parameters
	 *
	 */

	function redirect( $params = false ) {
		if ( !$params ) {
			$router = Smutty_Router::getInstance();
			$params = array(
				controller => $router->defController,
				action => $router->defAction
			);
		}	
		header( 'Location: ' . Smutty_Utils::getUrl($params) );
		Smutty_Main::completeRequest();
		exit();
	}

	/**
	 *  this function takes the same arguments as the normal
	 *  redirect function, but prints out javascript code
	 *  to do the redirect itself.  this can be useful when
	 *  doing ajax callbacks with forms.
	 *
	 *  it would be nice if i could think of a way to merge these
	 *  two methods and have it "know" which one to do, but that
	 *  seems a bit impossible i think?
	 *
	 *  @param [params] params for the redirect
	 *
	 */

	function redirectJs( $params ) {
		$url = Smutty_Utils::getUrl( $params );
		echo '<script type="text/javascript">self.location.href=\'' . $url . '\';</script>';
		exit();
	}

	/**
	 *  executes an action on the current controller
	 *
	 *  @param [name] name of action
	 *
	 */

	function action( $name ) {

		$router =& Smutty_Router::getInstance();
		$session =& Smutty_Session::getInstance();
		$data =& Smutty_Data::getInstance();

		// make sure the method exists
		$method = $name . 'Action';
		if ( !method_exists($this,$method) )
			new Smutty_Exception( ERR_ACTION_INVALID, 'ClassSmutty_Controller' );

		// mark this action as current
		$router->setActionName( $name );

		// call the action
		$this->$method(
			$data, $session
		);

	}

}

?>